PropertyDrawerBase.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using UnityEditor;
  2. using UnityEngine;
  3. namespace ExternPropertyAttributes.Editor
  4. {
  5. public abstract class PropertyDrawerBase : PropertyDrawer
  6. {
  7. public sealed override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
  8. {
  9. // Check if visible
  10. bool visible = PropertyUtility.IsVisible(property);
  11. if (!visible)
  12. {
  13. return;
  14. }
  15. // Validate
  16. ValidatorAttribute[] validatorAttributes = PropertyUtility.GetAttributes<ValidatorAttribute>(property);
  17. foreach (var validatorAttribute in validatorAttributes)
  18. {
  19. validatorAttribute.GetValidator().ValidateProperty(property);
  20. }
  21. // Check if enabled and draw
  22. EditorGUI.BeginChangeCheck();
  23. bool enabled = PropertyUtility.IsEnabled(property);
  24. using (new EditorGUI.DisabledScope(disabled: !enabled))
  25. {
  26. OnGUI_Internal(rect, property, PropertyUtility.GetLabel(property));
  27. }
  28. // Call OnValueChanged callbacks
  29. if (EditorGUI.EndChangeCheck())
  30. {
  31. PropertyUtility.CallOnValueChangedCallbacks(property);
  32. }
  33. }
  34. protected abstract void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label);
  35. sealed override public float GetPropertyHeight(SerializedProperty property, GUIContent label)
  36. {
  37. bool visible = PropertyUtility.IsVisible(property);
  38. if (!visible)
  39. {
  40. return 0.0f;
  41. }
  42. return GetPropertyHeight_Internal(property, label);
  43. }
  44. protected virtual float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
  45. {
  46. return EditorGUI.GetPropertyHeight(property, includeChildren: true);
  47. }
  48. protected float GetPropertyHeight(SerializedProperty property)
  49. {
  50. SpecialCaseDrawerAttribute specialCaseAttribute = PropertyUtility.GetAttribute<SpecialCaseDrawerAttribute>(property);
  51. if (specialCaseAttribute != null)
  52. {
  53. return specialCaseAttribute.GetDrawer().GetPropertyHeight(property);
  54. }
  55. return EditorGUI.GetPropertyHeight(property, includeChildren: true);
  56. }
  57. public virtual float GetHelpBoxHeight()
  58. {
  59. return EditorGUIUtility.singleLineHeight * 2.0f;
  60. }
  61. public void DrawDefaultPropertyAndHelpBox(Rect rect, SerializedProperty property, string message, MessageType messageType)
  62. {
  63. float indentLength = ExternalCustomEditorGUI.GetIndentLength(rect);
  64. Rect helpBoxRect = new Rect(
  65. rect.x + indentLength,
  66. rect.y,
  67. rect.width - indentLength,
  68. GetHelpBoxHeight());
  69. ExternalCustomEditorGUI.HelpBox(helpBoxRect, message, MessageType.Warning, context: property.serializedObject.targetObject);
  70. Rect propertyRect = new Rect(
  71. rect.x,
  72. rect.y + GetHelpBoxHeight(),
  73. rect.width,
  74. GetPropertyHeight(property));
  75. EditorGUI.PropertyField(propertyRect, property, true);
  76. }
  77. }
  78. }